package com.bookstore.service.impl;

import com.bookstore.domain.User;
import com.bookstore.mapper.AddressMapper;
import com.bookstore.mapper.UserMapper;
import com.bookstore.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper mapper;
    @Autowired
    private AddressMapper addressMapper;
    @Override
    public User login(User user) {
        return mapper.findByUsernameAndPassword(user);
    }

    @Override
    public Integer activation(String code) {
        return mapper.updateStatusByCode(code);
    }

    @Override
    public List<User> getUsers(Integer uType, String name) {
        return mapper.findAll(uType,name);
    }

    @Override
    public int upStatus(Integer status, Integer uid) {
        return mapper.upStatusByUid(status,uid);
    }

    @Override
    public int delUser(Integer uid) {

        int result1 = addressMapper.delAddresses(uid);
        int result2 = mapper.delUser(uid);
        if(result1!=0 && result2 != 0){
            return 1;
        }
        return 0;
    }

    @Override
    public int addUser(User user) {
        return mapper.addUser(user);
    }

    @Override
    public int upUser(User user) {
        return mapper.upUser(user);
    }

    @Override
    public User findById(Integer uid) {
        return mapper.findByUid(uid);
    }
}
